﻿using System;
using MadMediaManager.WPFSupport.Interfaces.Views;

namespace MadMediaManager.WPFSupport
{
    public abstract class ViewKey : IEquatable<ViewKey>
    {
        protected ViewKey(object viewData)
        {
            ViewData = viewData;
        }

        protected ViewKey(object viewData, ViewKey parent)
            : this(viewData)
        {
            Parent = parent;
        }

        public object ViewData { get; protected set; }
        public ViewKey Parent { get; protected set; }
        public abstract Type ViewType { get ; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj.GetType() == typeof(ViewKey) && Equals(obj as ViewKey);
        }

        public bool Equals(ViewKey other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.ViewType, ViewType) && Equals(other.ViewData, ViewData);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((ViewType != null ? ViewType.GetHashCode() : 0)*397) ^
                       (ViewData != null ? ViewData.GetHashCode() : 1);
            }
        }
    }

    public class ViewKey<TView> : ViewKey where TView : INavigableView
    {
        public ViewKey(object viewData) : base(viewData)
        {
        }

        public ViewKey(object viewData, ViewKey parent) : base(viewData, parent)
        {
        }

        public override Type ViewType { get { return typeof (TView); } }
    }
}
